package com.kingsoft.dc.khaos.extender.meta;

import com.alibaba.fastjson.JSON;
import com.kingsoft.dc.khaos.extender.meta.api.AuthRequest;
import com.kingsoft.dc.khaos.extender.meta.api.DatasourceListResult;
import com.kingsoft.dc.khaos.extender.meta.api.DmRequest;
import com.kingsoft.dc.khaos.extender.meta.impl.*;
import com.kingsoft.dc.khaos.extender.meta.model.MetaParams;
import com.kingsoft.dc.khaos.extender.meta.model.col.DmTableColumn;
import com.kingsoft.dc.khaos.extender.meta.model.db.DmDatabase;
import com.kingsoft.dc.khaos.extender.meta.model.ds.DmDatasource;
import com.kingsoft.dc.khaos.extender.meta.model.table.DmTable;
import com.kingsoft.dc.khaos.extender.meta.utils.RSAEncrypt;
import com.kingsoft.dc.khaos.extender.model.ParamConfig;

import java.util.List;

/**
 * Created by jing on 19/8/10.
 */
public class MetaClient {
    final static int DS_ID_NOT_FOULD = -9999;

    public MetaClient() {

    }

    public String getConnect(String config) {
        ParamConfig paramConfig = JSON.parseObject(config, ParamConfig.class);
        return getConnect(paramConfig);
    }

    public String getConnect(ParamConfig paramConfig) {
        DmMetaImpl dmMeta = null;
        String dsType = paramConfig.getDsType();
        if (dsType.equalsIgnoreCase("greenplum") || dsType.equalsIgnoreCase("hashdata") || dsType.equalsIgnoreCase("hawq")) {
            dmMeta = new MppDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("mysql")) {
            dmMeta = new MysqlDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("sqlserver")) {
            dmMeta = new SqlServerDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("oracle")) {
            dmMeta = new OracleDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("hive")) {
            dmMeta = new HiveDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("hbase")) {
            dmMeta = new HbaseDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("cos")) {
            dmMeta = new CosDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("kafka")) {
            dmMeta = new KafkaDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("elasticsearch")) {
            dmMeta = new EsDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("phoenix")) {
            dmMeta = new PhoenixDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("ks3")) {
            dmMeta = new Ks3DmMetaImpl();
        } else if (dsType.equalsIgnoreCase("hdfs")) {
            dmMeta = new HdfsDmMetaImpl();
        } else if (dsType.equalsIgnoreCase("pgsql")) {
            dmMeta = new PGSqlDmMetaImpl();
        }

        DmDatasource datasource = (DmDatasource) dmMeta.getDatasourceByName(createDatasourceRequest(paramConfig));
        return JSON.toJSONString(datasource.getConnect());
    }

    /**
     * 获取数据库属性
     *
     * @param config
     * @param dbName
     * @return
     */
    public String getDatabaseProps(String config, String dbName) {
        ParamConfig paramConfig = JSON.parseObject(config, ParamConfig.class);
        DmMetaImpl dmMeta = new DmMetaImpl();
        DmDatabase dbInfo = (DmDatabase) dmMeta.getDatabase(createDatabaseRequest(paramConfig, dbName));
        return JSON.toJSONString(dbInfo.getDbParams());
    }

    /**
     * 获取数据表属性
     *
     * @param config
     * @param dbName
     * @param tableName
     * @return
     */
    public String getTableProps(String config, String dbName, String tableName) {
        ParamConfig paramConfig = JSON.parseObject(config, ParamConfig.class);
        DmMetaImpl dmMeta = new DmMetaImpl();
        DmTable tableInfo = (DmTable) dmMeta.getTable(createTableRequest(paramConfig, dbName, tableName));
        return JSON.toJSONString(tableInfo.getParams());
    }

    /**
     * 获取数据表字段信息
     *
     * @param paramConfig
     * @param dbName
     * @param tableName
     * @return
     */
    public List<DmTableColumn> getTableColumns(ParamConfig paramConfig, String dbName, String tableName) {
        DmMetaImpl dmMeta = new DmMetaImpl();
        List<DmTableColumn> cols = (List) dmMeta.getColumns(createTableRequest(paramConfig, dbName, tableName));
        return cols;
    }

    /**
     * 获取数据表字段信息
     *
     * @param paramConfig
     * @return
     */
    public int getDatasourceId(ParamConfig paramConfig) {
        DmMetaImpl dmMeta = new DmMetaImpl();
        DatasourceListResult ds = dmMeta.getDatasourceList(createDatasourceListRequest(paramConfig));
        if (ds == null) {
            //TODO 或获取到DSID的处理
            return DS_ID_NOT_FOULD;
        }
        return ds.getId();
    }

    public DmRequest createDatasourceRequest(ParamConfig paramConfig) {
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(paramConfig.getAuthUrl());
        if (paramConfig.getAuthAk() != null) {
            authRequest.setAk(paramConfig.getAuthAk());
            authRequest.setSk(paramConfig.getAuthSk());
        }
        authRequest.setAuthProjectId(paramConfig.getJobProjectId());
        DmRequest dmRequest = new DmRequest();
        dmRequest.setAuthRequest(authRequest);
        dmRequest.setUrl(paramConfig.getUrl());
        MetaParams metaParams = new MetaParams();
        metaParams.buildWriteDatasourceParams(paramConfig.getRunEnv(), paramConfig.getDsName());
        dmRequest.setMetaParams(metaParams);
        return dmRequest;
    }

    public DmRequest createDatabaseRequest(ParamConfig paramConfig, String dbName) {
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(paramConfig.getAuthUrl());
        if (paramConfig.getAuthAk() != null) {
            authRequest.setAk(paramConfig.getAuthAk());
            authRequest.setSk(paramConfig.getAuthSk());
        }
        authRequest.setAuthProjectId(paramConfig.getJobProjectId());
        DmRequest dmRequest = new DmRequest();
        dmRequest.setAuthRequest(authRequest);
        dmRequest.setUrl(paramConfig.getUrl());
        MetaParams metaParams = new MetaParams();
        metaParams.setDsName(paramConfig.getDsName());
        metaParams.setDsId(paramConfig.getDsId());
        metaParams.setDsType(paramConfig.getDsType());
        metaParams.buildDatabaseParams(paramConfig.getRunEnv(), dbName);
        dmRequest.setMetaParams(metaParams);
        return dmRequest;
    }

    public DmRequest createTableRequest(ParamConfig paramConfig, String dbName, String tableName) {
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(paramConfig.getAuthUrl());
        if (paramConfig.getAuthAk() != null) {
            authRequest.setAk(paramConfig.getAuthAk());
            authRequest.setSk(paramConfig.getAuthSk());
        }
        authRequest.setAuthProjectId(paramConfig.getJobProjectId());
        DmRequest dmRequest = new DmRequest();
        dmRequest.setAuthRequest(authRequest);
        dmRequest.setUrl(paramConfig.getUrl());
        MetaParams metaParams = new MetaParams();
        metaParams.setDsId(paramConfig.getDsId());
        metaParams.setDsName(paramConfig.getDsName());
        metaParams.setDsType(paramConfig.getDsType());
        if (paramConfig.getDsType().equals("cos")) {
            metaParams.buildFileTableParams(paramConfig.getRunEnv(), dbName, tableName);
        } else {
            metaParams.buildNosqlTableParams(paramConfig.getRunEnv(), dbName, tableName);
        }

        dmRequest.setMetaParams(metaParams);
        return dmRequest;
    }

    public DmRequest createDatasourceListRequest(ParamConfig paramConfig) {
        AuthRequest authRequest = new AuthRequest();
        authRequest.setAuthUrl(paramConfig.getAuthUrl());
        if (paramConfig.getAuthAk() != null) {
            authRequest.setAk(paramConfig.getAuthAk());
            authRequest.setSk(paramConfig.getAuthSk());
        }
        authRequest.setAuthProjectId(paramConfig.getJobProjectId());
        DmRequest dmRequest = new DmRequest();
        dmRequest.setAuthRequest(authRequest);
        dmRequest.setUrl(paramConfig.getUrl());
        MetaParams metaParams = new MetaParams();
        metaParams.setDsName(paramConfig.getDsName());
        metaParams.setDsType(paramConfig.getDsType());
        metaParams.setEnv(paramConfig.getRunEnv());
        dmRequest.setMetaParams(metaParams);
        return dmRequest;
    }

    public static void main(String[] args) throws Exception {
        MetaClient client = new MetaClient();
        ParamConfig paramConfig = new ParamConfig();
        paramConfig.setRunEnv("TEST");
        paramConfig.setDsId(1);
        paramConfig.setDsType("");
        paramConfig.setAuthUrl("");
        paramConfig.setUrl("");
        String config = client.getConnect(paramConfig);

//        String config = "{\"ds_type\":\"mpp\",\"run_env\":\"test\",\"project_id\":\"149\",\"ds_id\":\"179\",\"server_auth_url\":\"http://10.69.56.153:8080/auth\",\"server_url\":\"http://10.69.56.159:80/dmgMdQuery/**\",}";

//        System.out.println(client.getConnect(config));
        String hive = "hN6mM2NUqSjYSJKizU+U/3EKUG6nExB6PeQ1yJtl192qA4dllw+Yt1QOIrIreZ8Qyfj1FxLAZwn";
        String mysql = "ZWuaQWv6a5/ApjLBvtSZtTS8vp/EE8ZRjOG0pjb/C6TXvruScWZrgcaAGnYP9jqe51VodfphkGQyVL9H3aZ+LOR1TOOSyn2VnlFDdkIw9KyQfEmTkQHd31R3Aq+6eDTF4IARIcWk6bFm5rwo7hey0Xvnc0f+9MuHmV3L7tMm1JY=";
        String passwd ="CXshK5rALVnwccMzTxZvWIsXRmR7nwKVc6d+RBaTUWDCxlFYJah+h5DPpZtIX3mhuxw6LQ2EucoH2nJZCMwKgDnoB+tH5OEIUlwSp82AWpNUXivUpcD0CbCdxeHycW9GqUjynP7AtOWDEzc3Rs+OtW4y31dSD7SkUmqMueLNDLE=";
        System.out.println("mysql:"+RSAEncrypt.decryptByRSAPassWord(passwd));   //python——mysql密码错误解密验证
    }
}
